<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%
    String data = input.getDesc("data", "");
    String title = input.getDesc("title", "");
    Integer surveyId = input.getInt("survey_id", -1);
    List<Survey> surveys = QBuilder.db("basedb").select().from("Survey").where(new Where("status = ", STATUS_ENABLE).and("enable=", STATUS_ENABLE).and("id=", surveyId)).query().getResultObj();
    if (isEmpty(surveys)) {
        msg = new Message(30050, "修改随访失败");
        Tools.encryptPrint(out, msg);
        return;
    }
    Survey survey = surveys.get(0);
    if (!"".equals(title)) {
        survey.setTitle(title);
        DBTools.db("basedb").modItem(survey, survey.getId());
    }
    List<SurveyQuestion> sqs = QBuilder.db("basedb").select().from("SurveyQuestion").where(new Where("status = ", STATUS_ENABLE).and("surveyId=", surveyId)).query().getResultObj();
    if (!isEmpty(sqs)) {
        for (int i = 0; i < sqs.size(); i++) {
            SurveyQuestion sq = sqs.get(i);
            sq.setStatus(STATUS_DISABLE);
        }
        DBTools.db("basedb").modItem(sqs);
    }
    if ("".equals(data)) {
        msg = new Message(30050, "修改随访失败");
        Tools.encryptPrint(out, msg);
        return;
    }
    String _data = "";
    try {
        _data = Tools.decryptStr(data);
    } catch(Exception e) {
        _data = "";
    }
    if ("".equals(_data)) {
        msg = new Message(30050, "修改随访失败");
        Tools.encryptPrint(out, msg);
        return;
    }
    List<Map> quests = JSONUtil.fromJson(_data, new TypeReference<List<Map>>(){});
    if (isEmpty(quests)) {
        msg = new Message(30050, "请至少添加一个随访内容");
        Tools.encryptPrint(out, msg);
        return;
    }
    Long time = new Date().getTime();
    for (int i = 0; i < quests.size(); i++) {
        Map quest = quests.get(i);
        SurveyQuestion sq = new SurveyQuestion();
        sq.setHospital(hospital);
        sq.setSurveyId(surveyId);
        Integer questShowOrder = Tools.getInt(quest.get("show_order") + "", i + 1);
        time = Tools.getLong(quest.get("time") + "", time);
        String schedule = Tools.getDesc(quest.get("schedule") + "", "");
        String unit = Tools.getDesc(quest.get("unit") + "", "");
        String questTitle = Tools.getDesc(quest.get("title"), "");
        String questDetail = Tools.getDesc(quest.get("detail"), "");
        if (questTitle.length() < 2) {
            msg = new Message(30050, "随访内容不能少于2个字符");
            Tools.encryptPrint(out, msg);
            return;
        }
        sq.setcTime(time);
        sq.setTitle(questTitle);
        sq.setDetail(questDetail);
        sq.setShowOrder(questShowOrder);
        sq.setType(0);
        sq.setJson("schedule", schedule);
        sq.setJson("unit", unit);
        Integer id = (Integer)DBTools.db("basedb").addItem(sq);
        if (isEmpty(id)) {
            msg = new Message(30050, "修改随访失败");
            Tools.encryptPrint(out, msg);
            return;
        }
    }
    msg.put("data", surveyId);
    Tools.encryptPrint(out, msg);
%>
